<template> 
  <div class="app-container">
    
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="用户名：">
            <el-input v-model="listQuery.nickname" class="input-width" placeholder="用户名" clearable></el-input>
          </el-form-item>
          <el-form-item label="手机号：">
            <el-input v-model="listQuery.phone" class="input-width" placeholder="手机号" clearable></el-input>
          </el-form-item> 
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="roleTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="头像" align="center">
          <template slot-scope="scope">
            <el-avatar :size="40" :src="scope.row.icon"></el-avatar>
          </template>
        </el-table-column> 
        <el-table-column label="昵称"  align="center">
          <template slot-scope="scope">{{scope.row.nickname}}</template>
        </el-table-column>
        <el-table-column label="注册时间"   align="center">
          <template slot-scope="scope">{{scope.row.createTime | formatDateTime}}</template>
        </el-table-column>
        <el-table-column label="手机号"   align="center">
          <template slot-scope="scope">{{scope.row.phone}}</template>
        </el-table-column>
        <el-table-column label="会员等级"  align="center">
          <template slot-scope="scope">
            {{scope.row.memberLevelName || '普通会员'}}
          </template>
        </el-table-column> 
        <!-- <el-table-column label="性别"  align="center">
          <template slot-scope="scope">
            {{ scope.row.gender === 1 ? '男' : '女' }}
          </template>
        </el-table-column>  -->
        <!-- <el-table-column label="状态"  align="center">
          <template slot-scope="scope">
            {{ scope.row.status === 1 ? '正常' : '拉黑' }}
          </template>
        </el-table-column>  -->
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope"> 
            <el-row>
             <el-button size="mini"
                       type="text"
                       @click="handleView(scope.row.id)">
              编辑用户
            </el-button>  
            <!-- <el-button size="mini"
                       type="text"
                       @click="handleDelete(scope.$index, scope.row)">拉黑
            </el-button> -->
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisible"
      width="40%">

      <el-form :model="formData"  ref="roleForm" label-width="150px" size="small">
        <!-- <el-form-item label="昵称：">
          <el-input v-model="formData.nickname" style="width: 300px"></el-input>
        </el-form-item>  
        <el-form-item label="手机号：">
          <el-input v-model="formData.phone" style="width: 300px"></el-input>
        </el-form-item>   -->
        <el-form-item label="会员等级：">
          <el-select v-model="formData.memberLevelId" multiple  placeholder="请选择" style="width: 300px">
            <el-option
              v-for="item in vipList"
              :key="item.id"
              :label="item.des"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态：">
          <el-switch v-model="formData.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleDialogConfirm">确认</el-button>
        <el-button @click="dialogVisible = false" size="small">关闭</el-button> 
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {list,edit,detail} from '@/api/member';
  import { list as vipTypeList } from '@/api/vipConfig'; 
  import {list as getCateList} from '@/api/articleCategory';
  import {formatDate} from '@/utils/date';
  import Tinymce from '@/components/Tinymce'

  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    nickname: null,
    phone: null,
  };
  const defaultData = {
    state: '',
    categoryId: '',
  };
  export default {
    name: 'articleList',
    components: {Tinymce},
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        formData: Object.assign({}, defaultData),
        isEdit: false,
        vipList:[],
        userId:'',
      }
    },
    created() {
      this.getList();
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    },
    methods: {  
      
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      }, 
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleView(id) {
        this.dialogVisible = true;
        this.isEdit = true;
        this.userId = id;
        detail({id}).then(response => {
          this.formData = response.data;
          this.formData.memberLevelId = response.data.memberLevelId.split(',').map(Number);
        });
        vipTypeList().then(response => {
          this.vipList = response.data.list;
        });
      },
      handleDelete(index, row) {
        this.$confirm('是否要禁用该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const data = {
            id: row.id,
            status: 0
          }
          edit(data).then(response => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList();
          });
        });
      },
      handleUpdate(index, row) {
        this.dialogVisible = true;
        this.isEdit = true;
        this.formData = Object.assign({},row);
        this.formData.id = row.id; 
      },
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.isEdit) {
            //status : this.formData.status,
            const result = {
              id:this.userId,
              memberLevelId : this.formData.memberLevelId.join(','), 
            }; 
            edit(result).then(response => {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          } else {
            add(this.formData).then(response => {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          }
        })
      },  
      getList() {
        this.listLoading = true;
        list(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      }
    }
  }
</script>
<style></style>


